import {Component} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {AdministratorEditComponent} from "../edit/administrator-edit.component";
import {AdministratorRoleComponent} from "../role/administrator-role.component";
import {GnDigestUtils, GnMessageService, GnTable} from "geminis-ng";
import {GnTableColumn} from "geminis-ng/data/table.component";

@Component({
  selector: 'gn-manage-administrator-list',
  templateUrl: './administrator-list.component.html'
})
export class AdministratorListComponent extends GnTable {

  typeEdit = AdministratorEditComponent;
  typeRole = AdministratorRoleComponent;

  columns: GnTableColumn[] = [
    {label: '登录名', field: 'id'},
    {label: '名称', field: 'name'},
    {label: '角色', field: 'age', formatter: row => row.roles.map(role => role.name).join(",")}
  ];

  constructor(private httpClient: HttpClient,
              private messageService: GnMessageService) {
    super();
  }

  resetPassword() {
    this.messageService.confirm('重置管理员密码?').subscribe(() => {
      this.table.selectedIds.forEach(admin => {
        GnDigestUtils.sha256(admin)
          .then(value => {
            this.httpClient.put('/MANAGE-SERVICE/administrators/' + admin + '/resetPassword?password=' + value, null)
              .subscribe(() => {
                this.messageService.success('密码已重置');
              }, error => {
                this.messageService.error({message: '密码重置错误', error: error});
              });
          });
      });
    });
  }
}

